import { Component } from "react";
import Taro from "@tarojs/taro";
import * as api from "@/src/pages/login/api";
import { setToken } from "@/src/service";
import * as collectApi from './pages/collectList/api'
import "./app.scss";

class App extends Component {
  // eslint-disable-next-line @typescript-eslint/no-useless-constructor
  constructor(props) {
    super(props);
  }

  componentDidMount() {

    const getCollectList = async () => {
      const { result, status } = await collectApi.customer_collection_getallcollectionsids({});
      if (status === "ok") {
        console.log("result---->>", result);
        Taro.setStorageSync('collectIds',result.goods_ids)
      }
    };
    const updateManager = Taro.getUpdateManager();
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      console.log(res.hasUpdate);
    });
    updateManager.onUpdateReady(function () {
      Taro.showModal({
        title: "更新提示",
        content: "新版本已经准备好，是否重启应用？",
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate();
          }
        },
      });
    });
    updateManager.onUpdateFailed(function () {
      // 新的版本下载失败
    });

    Taro.login({
      success: async (res) => {
        if (res.code) {
          try {
            const { result, status } = await api.customer_account_autologin({
              code: res.code,
            });
            if (status === "ok") {
              console.log("result------->>>", result);
              if (result.access_token) {
                getCollectList()
                setToken({
                  ...result,
                });
              }
            } else {
              console.log("新用户暂无token");
            }
          } catch (err) {
            console.log("err--------->>>", err);
          } finally {
          }
        } else {
          console.log("登录失败！" + res.errMsg);
        }
      },
    });
  }

  componentDidShow() {}

  componentDidHide() {}

  componentDidCatchError() {}

  // this.props.children 是将要会渲染的页面
  render() {
    return this.props.children;
  }
}

export default App;
